找传奇、传世资源到传世资源站!

android_发送接收短信例子源码_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

/*package com.example.sms;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
}*/
package com.example.sms;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
	private Button mButton;
	private EditText content;
	private EditText telNo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		telNo = (EditText) findViewById(R.id.telNo);
		content = (EditText) findViewById(R.id.content);
		mButton = (Button) findViewById(R.id.btn);

		telNo.setText("请输入电话号码");
		content.setText("请输入短信内容");
		// play.setOnClickListener(new Button.OnClickListener() {

		telNo.setOnClickListener(new EditText.OnClickListener() {
			public void onClick(View v) {
				telNo.setText("");
			}
		});
		content.setOnClickListener(new EditText.OnClickListener() {
			public void onClick(View v) {
				content.setText("");
			}
		});
		mButton.setOnClickListener(new Button.OnClickListener() {

		
			public void onClick(View arg0) {
				String strDestDddress = telNo.getText().toString();
				String strMessage = content.getText().toString();
				SmsManager smsManager = SmsManager.getDefault();
				String strDestAddress = null;
				if (isPhoneNumberValid(strDestAddress) == true
						&& iswithin70(strMessage) == true) {
					try {
						PendingIntent mPI = PendingIntent.getBroadcast(
								MainActivity.this, 0, new Intent(), 0);
						String strMessaage = null;
						smsManager.sendTextMessage(strDestAddress, null,
								strMessaage, mPI, null);
					} catch (Exception e) {
						e.printStackTrace();
					}
					Toast.makeText(MainActivity.this, "送出成功!", Toast.LENGTH_SHORT)
							.show();
					telNo.setText(" ");
					content.setText(" ");
				} else {
					if (isPhoneNumberValid(strDestAddress) == false) {
						if (iswithin70(strMessage) == false) {
							Toast.makeText(MainActivity.this,
									"电话号码格式错误 短信内容超过70字,请检查",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(MainActivity.this, "电话号码格式错误,请检查!!",
									Toast.LENGTH_SHORT).show();
						}
					} else if (iswithin70(strMessage) == false) {
						Toast.makeText(MainActivity.this, "短信内容超过70字,请删除部分内容!!",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

	private boolean isPhoneNumberValid(String strDestAddress) {
		boolean isValid = false;
		String expression1 ="^\\(?(\\d{3})\\)?[-]?(\\d{3})[-]?(\\d{4})$";
		String expression2 = "^\\(?(\\d{2})\\)?(\\d{4})[-]?(\\d{4})$";
		CharSequence phoneNumber = null;
		CharSequence inputStr = phoneNumber;
		Pattern pattern = null;
		Matcher matcher = pattern.matcher(inputStr);
		Pattern pattern2 = null;
		Matcher matcher2 = pattern2.matcher(inputStr);
		if(matcher.matches()||matcher2.matches()){
			isValid = true;
		}
		return isValid;
	}

	private boolean iswithin70(String text) {
		if(text.length()<=70)
			return true;
		else
			return false;
	}
	public String getSmslnPhone(){
		final String SMS_URL_ALL = "content://sms/";
		final String SMS_URL_INBOX = "contet://sms/inbox";
		final String SMS_URL_SEND = "content://sms/sent";
		final String SMS_URL_DRAFT = "content://sms/draft";
		StringBuilder smsBuilder = new StringBuilder();
		try{
			ContentResolver cr = getContentResolver();
			String[] projection = new String[]{"_id","address","peerson","body","date","date","type"};
			Uri uri = Uri.parse(SMS_URL_ALL);
			Cursor cur = cr.query(uri, projection, null, null, "date desc");
			if(cur.moveToFirst()){
				String name;
				String phoneNumber;
				String smsbody;
				String date;
				String type;
				int nameColumn = cur.getColumnIndex("person");
				int phoneNumberColumn = cur.getColumnIndex("address");
				int smsbodyColumn = cur.getColumnIndex("body");
				int dateColumn = cur.getColumnIndex("date");
				int typeColumn = cur.getColumnIndex("type");
				do{
					name = cur.getString(nameColumn);
					phoneNumber = cur.getString(phoneNumberColumn);
					smsbody = cur.getString(smsbodyColumn);
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					Date d= new Date(Long.parseLong(cur.getString(dateColumn)));
					date = dateFormat.format(d);
					int typeld = cur.getInt(typeColumn);
					if(typeld == 1){
						type = "接受";
					}else if(typeld ==2){
						type = "發送";
					}else{
						type = "";
					}
					smsBuilder.append("[");
					smsBuilder.append(name ",");
					smsBuilder.append(phoneNumber ",");
					smsBuilder.append(smsbody ",");
					smsBuilder.append(date ",");
					smsBuilder.append(type);
					smsBuilder.append("]");
					if(smsbody == null)smsbody = "";
				}while(cur.moveToNext());
				}else{
					smsBuilder.append("no result!");
			}
			smsBuilder.append("getSmslnPhone has executed!");
		}catch(SQLiteException ex){
			Log.d("SQLiteException in getSmslnPhone", ex.getMessage());
		}
		return smsBuilder.toString();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复